/*
对话管理类
*/
window._MANAGERTALK = {

    _TALKOBJ:null,

    _TALKINDEX:0,

    _SENCE:null,

    _MSGLABEL:null,

    _MSG:null,

    _MSGINDEX:0,

    showTalk:function(parentNode,talkObj,sence){
        _MANAGERTALK._SENCE=sence;
        _MANAGERTALK._TALKOBJ=talkObj;
        _MANAGERTALK._TALKINDEX=0;

        var talkLabelObj = _GRM.getNodeObj('talk');
        var talkLabel = talkLabelObj.node;
        _MANAGERTALK._MSGLABEL=talkLabel.getChildByName('talkLabel').getChildByName('message');

        if(talkLabelObj.isnew){

            var btnnext = talkLabel.getChildByName('nextbtn');
            var btnconfirm = talkLabel.getChildByName('selectbtns').getChildByName('confirmbtn');
            var btncancel = talkLabel.getChildByName('selectbtns').getChildByName('cancelbtn');

            btnnext.on(cc.Node.EventType.TOUCH_START, _MANAGERTALK.next, talkLabel);
            btnconfirm.on(cc.Node.EventType.TOUCH_START, _MANAGERTALK.confirm, talkLabel);
            btncancel.on(cc.Node.EventType.TOUCH_START, _MANAGERTALK.cancel, talkLabel);

        }

        _MANAGERTALK.refreshTalkLabel(talkLabel);
        parentNode.addChild(talkLabel);
    },


    refreshTalkLabel:function(talkLabel){

        if(
            _MANAGERTALK._TALKOBJ
            &&_MANAGERTALK._TALKOBJ.talk
            &&_MANAGERTALK._TALKOBJ.talk[_MANAGERTALK._TALKINDEX]
        ){

            var talkobj = _MANAGERTALK._TALKOBJ.talk[_MANAGERTALK._TALKINDEX];
            var face = talkobj.face;
            var name = talkobj.name;
            var msg = talkobj.msg;

            talkLabel.getChildByName('talkLabel').getChildByName('face')
            .getComponent(cc.Sprite).spriteFrame = _GRM.getFaceFrame(face);
            talkLabel.getChildByName('talkLabel').getChildByName('name')
            .getComponent(cc.Label).string = name;
            //talkLabel.getChildByName('talkLabel').getChildByName('message')
            //.getComponent(cc.Label).string = msg;

            _MANAGERTALK.msgAnimation(msg);

            if(
                _MANAGERTALK._TALKINDEX==_MANAGERTALK._TALKOBJ.talk.length-1
                &&_MANAGERTALK._TALKOBJ.choice
            ){
              
                talkLabel.getChildByName('nextbtn').active = false;
                talkLabel.getChildByName('selectbtns').active =true;
            }else{

                talkLabel.getChildByName('nextbtn').active = true;
                talkLabel.getChildByName('selectbtns').active =false;
            }

            return true;
        }else{
            return false;
        }

    },

    msgAnimation:function(msg){

        _MANAGERTALK._MSGLABEL.getComponent(cc.Label).string = "";

        _MANAGERTALK._MSG=msg;
        _MANAGERTALK._MSGINDEX=0;

        _MANAGERTALK._SENCE.unschedule(_MANAGERTALK.showMsg);

        var interval = 0.1;// 以1秒为单位的时间间隔
        var repeat = _MANAGERTALK._MSG.length - 1;// 重复次数 = 字符串长度 - 1
        var delay = 0.1;// 我们在1秒后开始运行
		
        _MANAGERTALK._SENCE.schedule(_MANAGERTALK.showMsg, interval, repeat, delay);

    },

    showMsg:function(){
        if(_MANAGERTALK._MSGINDEX<_MANAGERTALK._MSG.length){
            _MANAGERTALK._MSGLABEL.getComponent(cc.Label).string += _MANAGERTALK._MSG[_MANAGERTALK._MSGINDEX];
            _MANAGERTALK._MSGINDEX++;
        }
    },

    next:function(){
        if(_MANAGERTALK._MSGINDEX<_MANAGERTALK._MSG.length){
            _MANAGERTALK._MSGINDEX=_MANAGERTALK._MSG.length;
            _MANAGERTALK._MSGLABEL.getComponent(cc.Label).string = _MANAGERTALK._MSG;
        }else{
            _MANAGERTALK._TALKINDEX++;
            if(!_MANAGERTALK.refreshTalkLabel(this)){
                _MANAGERTALK.doAfterTalk();
                _GRM.putNode(this);
            }
        }
    },

    doAfterTalk:function(){

        //console.log('对话结束',_MANAGERTALK._TALKOBJ);
        var talkobj = _MANAGERTALK._TALKOBJ;
        var talkType = talkobj.talkType;
        var talkNpc = talkobj.talkNpc;

        if(talkType=="chessFightReward"){

            if(talkobj.levelup){
                _CHESSFIGHT.doLevelUp(talkobj.levelUpPlayer);
            }else{
                _CHESSFIGHT.doActionEnd();
            }
            
        }else if(talkType=="teachTalk"){

            _TTCH.afterRead();

        }

        /*
        if(talkType=="stealSuccess"){
            _TIPS.showMessage('盗窃成功:'+talkobj.talkparam.message,0);
        }else if(talkType=="stealFail"){
            _TIPS.showMessage('盗窃失败:'+talkobj.talkparam.message,0);
        }else if(talkType=="qiaozaSuccess"){
            _TIPS.showMessage('敲诈成功:'+talkobj.talkparam.message,0);
        }else if(talkType=="qiaozaFail"){
            _TIPS.showMessage('敲诈失败:'+talkobj.talkparam.message,0);
        }else if(talkType=="suohuiSuccess"){
            _TIPS.showMessage('索贿成功:'+talkobj.talkparam.message,0);
        }else if(talkType=="suohuiFail"){
            _TIPS.showMessage('索贿失败:'+talkobj.talkparam.message,0);
        }
        
        else if(talkType=="studyFight"){
            //_TIPS.showMessage('学技能战斗',0);
            _MANAGERTALK.jumpToFightOfType([window.gameData.playerTeam[0]],talkNpc,talkType);
        }else if(talkType=="killFight"){
            //_TIPS.showMessage('暗杀战斗',0);
            if(_HDLVHELPER.getHdLv()==0){
                _MANAGERTALK.jumpToFightOfType(window.gameData.playerTeam,talkNpc,talkType);
            }else{
                _MANAGERTALK.jumpToFightOfType([window.gameData.playerTeam[0]],talkNpc,talkType);
            }
        }else if(talkType=="killBossFight"){
            //_TIPS.showMessage('火并战斗',0);
            _MANAGERTALK.jumpToFightOfType([window.gameData.playerTeam[0]],talkNpc,talkType);
        }else if(talkType=="pkFight"){
            //_TIPS.showMessage('单挑战斗',0);
            _MANAGERTALK.jumpToFightOfType([window.gameData.playerTeam[0]],talkNpc,talkType);
        }else if(talkType == "zhaomuFight"){
            _MANAGERTALK.jumpToFightOfType([window.gameData.playerTeam[0]],talkNpc,talkType);
        }
        else if(talkType=="giveupJob"){
            _TIPS.showMessage('请辞成功',0);
        }else if(talkType=="getJobSuccess"){
            _TIPS.showMessage('应聘成功',0);
        }else if(talkType=="getJobFail"){
            _TIPS.showMessage('应聘失败',0);
        }else if(talkType=="getworkSuccess"){
            //_TIPS.showMessage('任务成功',0);
        }else if(talkType=="getworkFail"){
            //_TIPS.showMessage('任务失败',0);
        }
        else if(talkType=="zhaomuSuccess"){
            _TIPS.showMessage('招募成功',0);
        }
        else if(talkType=="zhaomuFail-1"){
            _TIPS.showMessage('招募失败:无法招募势力主公',0);
        }
        else if(talkType=="zhaomuFail-2"){
            _TIPS.showMessage('招募失败:无法招募等级超过主角100级的角色',0);
        }
        else if(talkType=="zhaomuFail-3"){
            _TIPS.showMessage('招募失败:无法招募非同阵营且非中立角色',0);
        }
        else if(talkType=="zhaomuFail-4"){
            _TIPS.showMessage('招募失败:友好未达到100或者此人已在队中',0);
        }
        else if(talkType=="diaoqianSuccess"){
            _TIPS.showMessage('调遣成功',0);
        }
        else if(talkType=="diaoqianFail-1"){
            _TIPS.showMessage('调遣失败:无法调遣主公',0);
        }else if(talkType=="diaoqianFail-2"){
            _TIPS.showMessage('调遣失败:上阵角色已满',0);
        }else if(talkType=="diaoqianFail-3"){
            _TIPS.showMessage('调遣失败:此人已在队中',0);
        }
        else if(talkType=="guyongSuccess"){
            _TIPS.showMessage('雇佣成功',0);
        }
        else if(talkType=="guyongFail-1"){
            _TIPS.showMessage('雇佣失败:非中立单位无法雇佣',0);
        }else if(talkType=="guyongFail-2"){
            _TIPS.showMessage('雇佣失败:上阵角色已满',0);
        }else if(talkType=="guyongFail-3"){
            _TIPS.showMessage('雇佣失败:需要名声大于被雇佣者的等级',0);
        }
        else if(talkType=="giveSuccess"){
            _TIPS.showMessage('友好度提升成功',0);
        }
        else if(talkType=="giveFail"){
            _TIPS.showMessage('友好度提升失败:每100000钱增加1点友好',0);
        }
        else if(talkType=="xinghuiSuccess"){
            _TIPS.showMessage('行贿成功',0);
        }
        else if(talkType=="xinghuiFail"){
            _TIPS.showMessage('行贿失败:每100000钱增加1点功勋',0);
        }else if(talkType=="guihuaSuccess"){
            _TIPS.showMessage('归化成功',0);
        }
        else if(talkType=="guihuaFail"){
            _TIPS.showMessage('归化失败:钱少于10000000',0);
        }else if(talkType=="giveItem"){

            _GAMEDATAHELPER.pushItem(_IOF.createObjByName(talkobj.item,null))
            _TIPS.showMessage('得到馈赠:'+talkobj.item,0);

        }else if(talkType=="gameEnd"){
            var gameEndType = talkobj.gameEndType;
            window._GAMEENDTYPE=gameEndType;
            _MAP.jumpToGameEnd();
        }else if(talkType=="gameStart"){
            window.gameData.gameStart=1;
        }else if(talkType=="gameStart2"){
            window.gameData.gameStartSencond=1;
        }else if(talkType=="fujifight"){
            _MAP.jumpToFightNoAutoSave();
        }else if(talkType=="hideStory"){

            //_HSTORYHELPER.createHideStoryFight(talkobj.story);
            _MAP.showHideStory(talkobj.story);
        }else if(talkType=="gameEnd2"){

            window._GAMEENDTYPE='sencondEnd';
            _MAP.jumpToGameEnd();
        }else if(talkType=="cutDarkLevel"){
            _MANAGERNMB.showNmbUi(_MAP.node,{type:_MANAGERNMB._TYPEJUANZEN});
        }
        */
    },

    cancel:function(){
        _GRM.putNode(this);
    },

    confirm:function(){
        _GRM.putNode(this);

        //console.log(_MANAGERTALK._TALKOBJ.choice);

        if(_MANAGERTALK._TALKOBJ.choice.type=="fight"){
            var player = _CU.cloneObj(_NPC[_MANAGERTALK._TALKOBJ.choice.target]);
            player.exp = window._EXPMANAGER.getExpByLevel(player.lv);
            _FIGHTHELPER.prepareFight(window.gameData.playerTeam,[player],{npc:_MANAGERTALK._TALKOBJ.choice.target,type:'pkfight'});
            _MAP.jumpToFight();
        }

    },

    jumpToFightOfType:function(playerTeam,npc,type){
        var player = null;

        if(_HDLVHELPER.getHdLv()==0){
            player = _ROF.createRole(npc);
        }else{
            player = _ROF.createRoleAndSetPlayerLv(npc);
        }

        _FIGHTHELPER.prepareFight(playerTeam,[player],{npc:npc,type:type});
        _MAP.jumpToFight();
    }

}